<script setup>
import { defineProps, defineEmits } from 'vue'

const props = defineProps({
  data: {
    type: Array,
    required: true
  },
  columns: {
    type: Array,
    required: true,
    default: () => [
      '用户编号',
      'BU',
      'Email',
      '启用时间',
      '停用时间',
      '排队中的任务数量',
      '进行中的任务数量',
      '操作'
    ]
  }
})

const emit = defineEmits(['switch-change'])
</script>

<template>
  <table class="user-table">
    <thead>
      <tr>
        <th v-for="col in columns" :key="col">
          {{ col }}
        </th>
      </tr>
    </thead>
    
    <tbody>
      <tr v-for="user in data" :key="user.userId">
        <td>{{ user.userId }}</td>
        <td>{{ user.bu }}</td>
        <td>{{ user.email }}</td>
        <td>{{ user.enableTime }}</td>
        <td>{{ user.disableTime }}</td>
        <td>{{ user.queuedTasks }}</td>
        <td>{{ user.runningTasks }}</td>
        <td>
          <label class="switch">
            <input 
              type="checkbox"
              @change="$emit('switch-change', user.userId, $event.target.checked)"
            >
            <span class="slider"></span>
          </label>
        </td>
      </tr>
    </tbody>
  </table>
</template>

<style scoped>
.user-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

th, td {
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #ebeef5;
}

th {
  background-color: #f5f7fa;
  font-weight: 600;
  color: #606266;
}

/* 与 Overview.vue 一致的开关样式 */
.switch {
  position: relative;
  display: inline-block;
  width: 40px;
  height: 20px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  transition: .4s;
  border-radius: 20px;
}

input:checked + .slider {
  background-color: #67c23a;
}

.slider:before {
  content: "";
  height: 16px;
  width: 16px;
  left: 2px;
  bottom: 2px;
  background-color: white;
  transition: .4s;
  border-radius: 50%;
  position: absolute;
}

input:checked + .slider:before {
  transform: translateX(20px);
}

@media (max-width: 768px) {
  td, th {
    padding: 8px 10px;
  }
}
</style>
